const Koa = require("koa");
const Router = require("koa-router");
const mockList = require("./mock/index");
const app = new Koa();
const router = new Router();
const { lastValueFrom, timer, map } = require("rxjs");
async function mockTimer(fn, ctx, time = 2000) {
  time = Math.random() * time;
  return lastValueFrom(timer(time).pipe(map(() => fn(ctx))));
}
mockList.forEach((item) => {
  const { url, method, response } = item;
  router[method](url, async (ctx) => {
    const res = await mockTimer(response, ctx);
    ctx.body = res;
  });
});
router.get("/", (ctx) => {
  ctx.body = {
    node: "start",
  };
});
app.use(router.routes());
app.listen(3001);
